<?php
require_once APP . '/application/controllers/UserAction.php';
require_once(APP . '/application/models/MeetModel.php');
require_once(APP . '/application/models/Article.php');
require_once(APP . '/application/models/UserModel.php');

/**
 * 会议管理主页
 * @package admin
 */
class ManageController extends UserAction
{
    private $mid;
    public function init()
    {
        $mid = (int)$this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	
    	$this->mid = $mid;
    	
    	if ($mid != 0 && $uid != 0){
    		$exists = $this->db->fetchOne("select count(*) from meet where mid=? and uid=?", array($mid, $uid));
    		if ($exists == false){
    			exit("该会议不是你创建的！");
    		}
    	}
    	
    	$this->view->mid = $mid;
    }
    
    /**
     * 首页设置
     */
    public function indexAction()
    {
        $mid = $this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	 
    	$obj_meet = new MeetModel($this->db);
    	 
    	$content = $obj_meet->show($mid);
    	if ($content['uid'] != $uid){
    		exit("该会议不是你创建的!");
    	}
    	
   		if ($this->getParam("homeuuid") != null){
   			$changeparam = array("homeuuid" => $this->getParam("homeuuid"));
   			if ($this->getParam("enhomeuuid"))
   				$changeparam['enhomeuuid'] = $this->getParam("enhomeuuid");
   			
    		$obj_meet->change($mid, $changeparam);

    		$content = $obj_meet->show($mid);
    		$this->view->setflag = true;
    	}

    	$content['top'] = @unserialize($content['top']);
    	$content['left'] = @unserialize($content['left']);
    	
    	$this->view->content = $content;
    	$this->render("index");
    }
    
    
    /**
     * 邮箱设置
     */
    public function mailAction()
    {
    	$mid = $this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	
    	$obj_meet = new MeetModel($this->db);
    	
    	$content = $obj_meet->show($mid);
    	if ($content['uid'] != $uid){
    		exit("该会议不是你创建的!");
    	}
    	
    	if ($this->getParam("email") != null)
    	{
    		$change = array();
    		$change['email'] = $this->getParam("email");
    		$change['emailuser'] = $this->getParam("emailuser");
    		$change['emailpwd'] = $this->getParam("emailpwd");
    		$change['smtpserver'] = $this->getParam("smtpserver");
    		$obj_meet->change($mid, $change);
    		$content = $obj_meet->show($mid);
    		$this->view->setflag = true;
    	}
    	
    	$this->view->content = $content;
    	$this->render("mail");
    }
    
    /**
     * 提示配置
     */
    public function ctipAction()
    {
		$tip_cn = $this->getParam("tip_cn");
		$tip_en = $this->getParam("tip_en");
		
		if ($tip_cn != null || $tip_en != null)
		{
			require_once(APP . '/application/models/PlatsetModel.php');
			$set = new PlatsetModel($this->db);

			$set->set("meet_{$this->mid}_ctip_cn", $tip_cn);
			$set->set("meet_{$this->mid}_ctip_en", $tip_en);
			$this->view->setflag = true;
		}
				
    	$this->view->tip_cn = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_cn'");
    	$this->view->tip_en = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_en'");
    	
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	
    	$this->view->language = $minfo['language'];
    	$this->render("ctip");
    }
    
    
    /**
     * 用户列表
     */
    public function ulistAction()
    {
    	$mid = (int)$this->getParam("mid");    	
    	$nowp = (int)$this->getParam("nowp");
    	$ischeck = (int)$this->getParam("ischeck");
    	$ischange = (int)$this->getParam("ischange", -1);
    	
    	$per_page = 20;
    	
    	$where = ' where mid=' . $mid;
    	if ($ischeck > 0)
    		$where = $where . ' and ischeck=' . $ischeck;
    	
    	if ($ischange != -1){
    		$where .= ' and ischange=' . $ischange;
    	}
    	
    	$totalNum = $this->db->fetchOne ('select count(*) from meetuser t1' . $where);
    	
    	require_once APP . '/application/models/Util.php';
    	
    	$pageInfo = Util::page ($totalNum, $per_page, $nowp);
    	$start    = $pageInfo ['start'];
    	$list     = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid
    			' . $where .' order by t1.regtime desc limit ' . $start . ',' . $per_page);
    	    	
    	$this->view->list = $list;
    	$this->view->page = $pageInfo['page'];
    	$this->view->mid = $mid;
    	$this->view->ischeck = $ischeck;
    	
    	$this->render("ulist");
    }
    

    /**
     * 用户列表
     */
    public function usearchAction()
    {
    	$mid = (int)$this->getParam("mid");
    	
    	$where = ' where mid=' . $mid;
    	     	
    	
    	if ($_POST['email']){
    
    		$where .= ' and email like "%' . strtolower($_POST['email']) . '%"';
    	$list     = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid
    			' . $where .' order by t1.regtime desc');
    	
    	$this->view->email = $_POST['email'];
    	}else{
    		$list = array();
    	}
    	$this->view->list = $list;
    	$this->view->mid = $mid;
    	 
    	$this->render("usearch");
    }
    
    
    /**
     * 显示用户信息
     */
    public function ushowAction()
    {
    	$uid = $this->getParam("uid");
    	$user = new UserModel($this->db);

    	$u_regitems = $this->db->fetchOne("select regitems from meetuser where mid=? and uid=?", array($this->mid, $uid));
    	$arr_u_items = unserialize($u_regitems);

    	$regitems = $this->db->fetchOne("select regitems from meet where mid=?", array($this->mid));
    	$regitems = unserialize($regitems);
    	
    	if (!is_array($regitems))
    		$regitems = array();
    	
    	$arr = array();
    	foreach ($regitems as $v){
    		if ($v['type'] <= 6){
    			$arr[] = $v;
    			
    			if ($v['type'] == 4){
    				$arr_u_items[$v['uuid']] = implode(",", $arr_u_items[$v['uuid']]);
    			}
    		}
    	}
    	
    	$this->view->content = $user->show($uid);
    	$this->view->regitem = $arr;
    	$this->view->uitems = $arr_u_items;
    	
    	$this->render("ushow");
    }
    
    /**
     * 修改平台用户信息
     */
    public function upchangeAction()
    {
    	$uid = (int)$this->getParam("uid");
    	$mid = (int)$this->getParam("mid");
    	
    	$exists = $this->db->fetchOne("select count(*) from meetuser where mid=? and uid=?", array($mid, $uid));
    	
    	if (empty($exists)){
    		$this->showtipmessage("", "该用户不是会议注册用户");
    		return;
    	}
    	

    	if (!empty($_POST['form_submit']))
    	{
    		$user = new UserModel($this->db);
    		$params = $this->getAllParams();
    		unset($params['password']);
    		
    		$user->change($uid, $params);
    		$this->view->change = true;
    	}

    	// 国家或地区读取
    	$nations = $this->db->fetchOne("select value from platset where name='plat_cn_nations'");
    	$arr_nations = explode("\n", $nations);
    	foreach($arr_nations as $index => $v){
    		$arr_nations[$index] = trim($v);
    		if (empty($arr_nations[$index])){
    			unset($arr_nations[$index]);
    		}
    	}

    	$this->view->nations = $arr_nations;
    	$this->view->dnation = $this->db->fetchOne("select value from platset where name='plat_cn_def_nation'");;
    	$this->view->mid = $mid;
    	$this->view->uid = $uid;
    	
    	$this->uinfo = $this->db->fetchRow("select * from user where uid=?", array($uid));
    	
    	$this->view->uinfo = $this->uinfo;
    	    	
    	$this->render("upchange");
    }
    
    
    /**
     * 修改用户信息
     */
    public function uchangeAction()
    {
    	$uid = (int)$this->getParam("uid");
    	$user = new UserModel($this->db);
    
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	$regitems = $minfo['regitems'];
    	$regitems = unserialize($regitems);
    	
    	if (!empty($_POST['form_submit']))
    	{
    		$u_regitems = $this->db->fetchOne("select regitems from meetuser where mid=? and uid=?", array($this->mid, $uid));
    		$arr_u_items = unserialize($u_regitems);
    		if (!is_array($arr_u_items))
    			$arr_u_items = array();
    		
    		foreach($regitems as $v){
    			//if (!$v['show'])continue;
    			if ($v['type'] > 6)continue;// 描述和分割线
    			if (isset($_POST[$v['uuid']]))
    				$arr_u_items[$v['uuid']] = $_POST[$v['uuid']];
    			if ($v['type'] == 4){
    				if (!isset($_POST[$v['uuid']])){
    					$arr_u_items[$v['uuid']] = "";
    				}
    			}
    		}
    		
    		$newValues = array("regitems" => serialize($arr_u_items));
    		$this->db->update("meetuser", $newValues, "mid={$this->mid} and uid={$uid}");
    		
    		$this->view->change = true;
    	}
    	
    	
    	
    	$u_regitems = $this->db->fetchOne("select regitems from meetuser where mid=? and uid=?", array($this->mid, $uid));

    
    	
    	$arr_u_items = unserialize($u_regitems);
    	if (!is_array($arr_u_items))
    		$arr_u_items = array();
    	 

    	$this->view->uitems = $arr_u_items;
    	$this->view->regitems = $regitems;
    	$this->view->uid = $uid;
    	$this->view->mid = $this->mid;
    	    	 
    	$this->render("uchange");
    }
    
    /**
     * 删除会议注册用户
     */
    public function udelAction()
    {
    	$uid = (int)$this->getParam("uid");
    	
    	$this->db->delete("meetuser", "mid=" . $this->mid . ' and uid=' . $uid);
    	
    	echo "T";
    }
    
    /**
     * 审核用户
     */
    public function ucheckAction()
    {
    	$uid = (int)$this->getParam("uid");
    	$ischeck = (int)$this->getParam("ischeck");
    	 
    	$uinfo = $this->db->fetchRow("select * from user where uid=?", array($uid));
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	
    	
    	$this->db->update("meetuser", array("ischeck" => $ischeck, "ischange" => 0), "mid=" . $this->mid . ' and uid=' . $uid);

    	// 发送邮件
    	$param = array();
    	$param['mid'] = $this->mid;
    	$param['uid'] = $uid;
    	$param['email'] = $uinfo['email'];
    	$param['ischeck'] = $ischeck;
    	$param['url'] = $this->view->fullUrl . '/' . $minfo['domain'];
    	
    	Util::callbackcmd("meet_check.php", $param);
    	
    	echo "T";
    }
    
    /**
     * 发送邮件
     */
    public function umailAction()
    {
    	$uid = (int)$this->getParam("uid");
    	$ischeck = $this->db->fetchOne("select ischeck from meetuser where mid=" . $this->mid . ' and uid=' . $uid);
    	
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	$uinfo = $this->db->fetchRow("select * from user where uid=?", array($uid));
    	
    	$content = "";
    	$this->view->title = "";
    	
    	$this->view->content = $content;
    	$this->view->mid = $this->mid;
    	
    	$this->view->email = $uinfo['email'];
    	$this->render("umail");
    }
    
    /**
     * 真正发送邮件
     */
    public function domailAction()
    {
    	$mid = $this->getParam("mid");
		
		// 获取会议信息
		$minfo = $this->db->fetchRow("select * from meet where mid=?", array($mid));
		$meeturl = $this->view->fullUrl . '/' . $minfo['domain'];
		
    	$title = $this->getParam("title");
    	$content = $this->getParam("content");
		if ($minfo['language']==2)
		{
		  $content .= '<br /><p>&nbsp;</p>--------------------<p>'.$minfo['name'].'会议注册系统：<a href="'.$meeturl.'?lan=cn">'.$meeturl.'</a>';
		  $content .= '<br />'.$minfo['enname'].' Conference Service Platform: <a href="'.$meeturl.'?lan=en">'.$meeturl.'</a></p>';
		}else{
		  $content .= '<br /><p>&nbsp;</p>--------------------<p>'.$minfo['name'].'会议注册系统：<a href="'.$meeturl.'">'.$meeturl.'</a></p>';
		}  
    	$email = $this->getParam("email");
    	
    	// 开始发送
    	$param = array();
    	$param['mid'] = $mid;
    	$param['email'] = $email;
    	$param['title'] = $title;
    	$param['content'] = $content;
    	$param['ccmail'] = $this->getParam("ccmail");
    	    	
    	Util::callbackcmd("manage_mail.php", $param);
    	
    	echo '{"status":1}';
    }
    
    
    /**
     * 导出用户
     */
    public function uexportAction()
    {
    	$mid = $this->getParam("mid");
    	$type = $this->getParam("type");
    	
    	$arr_old_files = array();
    	if (file_exists(APP . '/export/' . $mid . '/' . $type)){
    		$arr_files = scandir ( APP . '/export/' . $mid . '/' . $type, 1);
    		foreach($arr_files as $v){
    			list( $dirname, $basename, $extension, $filename ) = array_values( pathinfo($v) );
    			if ($extension == 'xlsx'){
    				$arr_old_files[] = $basename;
    			}
    		}
    	}
    	
    	$this->view->files = $arr_old_files;
    	$this->view->mid = $mid;;
    	$this->view->type = $type;
    	$this->render("uexport");
    }
    
    /**
     * 导出用户
     */
    public function douexportAction()
    {
    	set_time_limit(0);
    	ini_set('memory_limit', '-1');
    	
    	$mid = $this->getParam("mid");
    	$type = $this->getParam("type");
    	$compare = $this->getParam("compare");
    	
    	$uinfo = $this->db->fetchRow("select * from user where uid=?", array($this->uid));
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($mid));
    	
    	$filename = $minfo['domain'] . '_' . date("md") . '_' . time();
    	$regitems = $minfo['regitems'];
    	$regitems = unserialize($regitems);
    	 
    	if (!is_array($regitems))
    		$regitems = array();
    	
    	$m_reg_items = array();
    	foreach ($regitems as $v){
    		if ($v['type'] <= 6){
    			$m_reg_items[] = $v;
    		}
    	}
    	
    	if ($type != 0)
		{
		  $list = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid 
    			where t1.mid=? and t1.ischeck=?', array($mid, $type));
		}
		else 
		{
		  $list = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid 
    			where t1.mid=?', array($mid));
		}
    	/**
    	for($i=0; $i < 500; $i++){
    		$tmp = $list[0];
    		$tmp['uid'] = $i;
    		$list[] = $tmp;
    	}**/

    	if (!file_exists(APP . '/export/' . $mid))
    		@mkdir(APP . '/export/' . $mid);
    	
    	if (!file_exists(APP . '/export/' . $mid . '/' . $type))
    		@mkdir(APP . '/export/' . $mid . '/' . $type);    	
    	
    	
    	// 生成数据, 行列
		$mast_fields = array('uid', 'email', 'enfname', 'enlname', 'name', 'gender',
				'nation', 'birth', 'idcode', 'unit','cnunit', 'headphoto','idphoto', 'phone', 'fax', 'address', 'pcode');
		
		$mast_fields_msg = array('用户编号','邮件地址', '英文姓', '英文名', '本国姓名', '性别',
				'国家或地区', '出生日期', '身份证号码', '工作单位（中文名称）','工作单位（英文名称）','职务（中文名称）','职务（英文名称）', '联系电话', '传真', '地址', '邮编');

		$result = array("keys"=>array(), "list"=>array());
		$result['keys'] = $mast_fields_msg;
		
		// 合并会议注册项
		foreach($m_reg_items as $item){
			$result['keys'][] = $item['title'];
		}
		
		// 按拼音排序处理
		require_once APP . '/application/models/PinYin.php';
		$obj_py = new PinYin();
		foreach($list as $index => $v){
			$list[$index]['py'] = $obj_py->getPinYin($v['name'], "", false);// 拼音
		}
		
		$piny = array();
		foreach ($list as $key => $row) {
			$piny[$key]  = $row['enfname'];
		}
		
		array_multisort($piny, SORT_ASC, $list);
				
		// 输出字段处理
		foreach($list as $v){
			$u_regitems = $v['regitems'];
			$u_regitems = unserialize($u_regitems);
			
			$row = array();
			// 基本用户信息
			foreach($mast_fields as $key){
				$row[] = $v[$key];
			}
			// 会议注册信息
			foreach($m_reg_items as $item){
				if ($item['type'] == 4){
					$mvalue = implode(",", $u_regitems[$item['uuid']]);
				}else{
					$mvalue = $u_regitems[$item['uuid']];
				}
				$row[] = $mvalue;
			}
			
			$result['list'][] = $row;
		}
		
		
		// 保存结果
		file_put_contents(APP . '/export/' . $mid . "/$type/$filename.json", json_encode($result));
		
		/**
		 * 对比处理
		 */
		if (!empty($compare) && file_exists(APP . '/export/' . $mid . "/$type/$compare.json"))
		{
			$arr_old_result = json_decode(file_get_contents(APP . '/export/' . $mid . "/$type/$compare.json"), true);
			
			$arr_old_val = array();
			foreach($arr_old_result['list'] as $index => $v){
				$arr_item_val = array();
				foreach($arr_old_result['keys'] as $t_index => $vv)
					$arr_item_val[$vv] = $v[$t_index];
				$arr_old_val[$arr_item_val['用户编号']] = $arr_item_val;
			}	
		}

    	// 导入excel
    	include APP . '/application/models/PHPExcel.php';
    	include APP . '/application/models/PHPExcel/IOFactory.php';
    	 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()
		->setTitle($minfo['name'] . '名单')
		->setCreator($uinfo['name'])
		->setLastModifiedBy($uinfo['name']);
		
		$objPHPExcel->setActiveSheetIndex(0);
				
		// 设置标题
		for($i=0; $i < count($result['keys']); $i++){
			// 标题样式
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($i, 1)->getFont()->setBold(true);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($i, 1)->getFont()->setSize(12);
			
			// 列宽设置
			$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($i)->setWidth(24);
			
			// 设置值
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $result['keys'][$i]);
		}
		
		// 设置内容
		for($i=0; $i < count($result['list']); $i++){

			$puid = $result['list'][$i][0];
			$change_flag = false;
			
			for($col=0; $col < count($result['keys']); $col++){
				$pvalue = $result['list'][$i][$col];
				$pkey = $result['keys'][$col];
								
				$change = false;
				if (isset($arr_old_val[$puid])){
					if ($arr_old_val[$puid][$pkey] != $pvalue){
						$change = true;
						$change_flag = true;
					}
				}
				
				if ($result['keys'][$col] == $mast_fields_msg[5]){
					$pvalue = $pvalue == 1?'男':'女';
				}

				$objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($col, $i+2, $pvalue, PHPExcel_Cell_DataType::TYPE_STRING);
				
				if ($change){
					$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col, $i+2)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
					$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col, $i+2)->getFill()->setStartColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED));
				}
			}
			
			// 如果有修改，设置uid为红色
			if ($change_flag){
				$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(0, $i+2)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
				$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(0, $i+2)->getFill()->setStartColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED));
			}
		}
		
		
		$objPHPExcel->setActiveSheetIndex(0);
		$objPHPExcel->getActiveSheet()->setTitle('名单');
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save(APP . '/export/' . $mid . "/$type/$filename.xlsx");
		
		header("location:" . $this->view->fullUrl . '/export/' . $mid . "/$type/$filename.xlsx");
    }
    
    /**
     * 分组导出用户
     */
    public function ugexportAction()
    {
    	$mid = $this->getParam("mid");
    	$type = $this->getParam("type");
    	 
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($mid));

    	$mast_fields = array('uid', 'email', 'enfname', 'enlname', 'name', 'gender',
    			'nation', 'birth', 'idcode', 'unit','cnunit', 'headphoto','idphoto', 'phone', 'fax', 'address', 'pcode');
    	
    	$mast_fields_msg = array('用户编号','邮件地址', '英文姓', '英文名', '本国姓名', '性别',
    			'国家或地区', '出生日期', '身份证号码', '工作单位（中文名称）','工作单位（英文名称）','职务（中文名称）','职务（英文名称）', '联系电话', '传真', '地址', '邮编');
    	
    	$arr_fields = array();
    	for($i=0; $i < count($mast_fields); $i++)
    		$arr_fields[$mast_fields[$i]] = $mast_fields_msg[$i];
    	
    	$regitems = $minfo['regitems'];
    	$regitems = unserialize($regitems);
    	
    	if (!is_array($regitems))
    		$regitems = array();
    	 
    	$m_reg_items = array();
    	foreach ($regitems as $v){
    		if ($v['type'] <= 6){
    			$arr_fields[$v['uuid']] = $v['title'];
    		}
    	}
    	 
    	$this->view->fields = $arr_fields;
    	$this->view->mid = $mid;
    	$this->view->type = $type;
    	$this->render("ugexport");
    }
    
    
    /**
     * 分组导出用户
     */
    public function dougexportAction()
    {
    	set_time_limit(0);
    	ini_set('memory_limit', '-1');
    	 
    	$mid = $this->getParam("mid");
    	$type = $this->getParam("type");
    	
    	$gkey = $this->getParam("gkey");
    	$gindex = 0;
    	 
    	$uinfo = $this->db->fetchRow("select * from user where uid=?", array($this->uid));
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($mid));
    	 
    	$filename = $minfo['domain'] . '_' . date("md") . '_' . time();
    	$regitems = $minfo['regitems'];
    	$regitems = unserialize($regitems);
    
    	if (!is_array($regitems))
    		$regitems = array();
    	 
    	$m_reg_items = array();
    	foreach ($regitems as $v){
    		if ($v['type'] <= 6){
    			$m_reg_items[] = $v;
    		}
    	}
    	 
    	if ($type != 0)
		{
		  $list = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid
    			where t1.mid=? and t1.ischeck=?', array($mid, $type));
		}
		else
		{
		  $list = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid
    			where t1.mid=?', array($mid));
		}
    	/**
    	 for($i=0; $i < 500; $i++){
    	 $tmp = $list[0];
    	 $tmp['uid'] = $i;
    	 $list[] = $tmp;
    	}**/
    	 
    	if (!file_exists(APP . '/export/' . $mid))
    		@mkdir(APP . '/export/' . $mid);
    	 
    	// 生成数据, 行列
    	$mast_fields = array('uid', 'email', 'enfname', 'enlname', 'name', 'gender',
    			'nation', 'birth', 'idcode', 'unit','cnunit', 'headphoto','idphoto', 'phone', 'fax', 'address', 'pcode');
    	
    	$mast_fields_msg = array('用户编号','邮件地址', '英文姓', '英文名', '本国姓名', '性别',
    			'国家或地区', '出生日期', '身份证号码', '工作单位（中文名称）','工作单位（英文名称）','职务（中文名称）','职务（英文名称）', '联系电话', '传真', '地址', '邮编');
    	
    	$result = array("keys"=>array(), "list"=>array());
    	$result['keys'] = $mast_fields_msg;
    	
    	foreach($mast_fields as $index => $v){
    		if ($v == $gkey)
    			$gindex = $index;
    	}
    
    	// 合并会议注册项
    	foreach($m_reg_items as $item){
    		$result['keys'][] = $item['title'];
    		
    		if($item['uuid'] == $gkey)
    			$gindex = count($result['keys']) -1;
    	}
    
    	// 按拼音排序处理
    	require_once APP . '/application/models/PinYin.php';
    	$obj_py = new PinYin();
    	foreach($list as $index => $v){
    		$list[$index]['py'] = $obj_py->getPinYin($v['name'], "", false);// 拼音
    	}
    
    	$piny = array();
    	foreach ($list as $key => $row) {
    		$piny[$key]  = $row['enfname'];
    	}
    
    	array_multisort($piny, SORT_ASC, $list);
    
    	// 输出字段处理
    	foreach($list as $v){
    		$u_regitems = $v['regitems'];
    		$u_regitems = unserialize($u_regitems);
    			
    		$row = array();
    		// 基本用户信息
    		foreach($mast_fields as $key){
    			$row[] = $v[$key];
    		}
    		// 会议注册信息
    		foreach($m_reg_items as $item){
    			if ($item['type'] == 4){
    				$mvalue = implode(",", $u_regitems[$item['uuid']]);
    			}else{
    				$mvalue = $u_regitems[$item['uuid']];
    			}
    			$row[] = $mvalue;
    		}
    			
    		$result['list'][] = $row;
    	}
    	
    	// 分组处理
    	$newresult = array();
    	foreach ($result['list'] as $v){
    		$pvalue = $v[$gindex];
    		
    		if (isset($newresult[$pvalue])){
    			$newresult[$pvalue]['list'][] = $v;
    		}else{
    			$newresult[$pvalue] = array("keys" => $result['keys'], "list" => array());
    			$newresult[$pvalue]['list'][] = $v;
    		}
    	}
    	
    	// 导入excel
    	include APP . '/application/models/PHPExcel.php';
    	include APP . '/application/models/PHPExcel/IOFactory.php';
    
    	$objPHPExcel = new PHPExcel();
    	$objPHPExcel->getProperties()
    	->setTitle($minfo['name'] . '名单')
    	->setCreator($uinfo['name'])
    	->setLastModifiedBy($uinfo['name']);
    
    	$sheet_index = 0;
    	foreach($newresult as $sheet_name => $result){
    	if ($gkey == 'gender'){
    		$sheet_name = $sheet_name == 1?'男':'女';
    	}
    	
    	$objPHPExcel->createSheet($sheet_index);
    	$objPHPExcel->setActiveSheetIndex($sheet_index);
    
    	// 设置标题
    	for($i=0; $i < count($result['keys']); $i++){
    		// 标题样式
    		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($i, 1)->getFont()->setBold(true);
    		$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($i, 1)->getFont()->setSize(12);
    			
    		// 列宽设置
    		$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($i)->setWidth(24);
    			
    		// 设置值
    		$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $result['keys'][$i]);
    	}
    
    	// 设置内容
    	for($i=0; $i < count($result['list']); $i++){
    		$puid = $result['list'][$i][0];    			
    		for($col=0; $col < count($result['keys']); $col++){
    			$pvalue = $result['list'][$i][$col];
    			$pkey = $result['keys'][$col];
    
    			if ($result['keys'][$col] == $mast_fields_msg[5]){
    				$pvalue = $pvalue == 1?'男':'女';
    			}
    
    			$objPHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($col, $i+2, $pvalue, PHPExcel_Cell_DataType::TYPE_STRING);
    		}
    	}

    	$objPHPExcel->setActiveSheetIndex($sheet_index);
    	$objPHPExcel->getActiveSheet()->setTitle($sheet_name);
    	$sheet_index++;
    	}
    	
    	
    	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    	
    	ob_end_clean ();
    	header ( "Content-Type: application/force-download" );
    	header ( "Content-Type: application/octet-stream" );
    	header ( "Content-Type: application/download" );
    	header ( "Content-Disposition: attachment; filename=" . $filename . ".xlsx" );
    	header ( "Content-Transfer-Encoding: binary" );
    	header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
    	header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
    	header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
    	header ( "Pragma: no-cache" );
    	$objWriter->save ( 'php://output' );
    	//$objWriter->save(APP . '/export/' . $mid . "/$filename.xlsx");
    }
    
    /**
     * 设置
     */
    public function setnlAction()
    {
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	 
    	$regitems = $minfo['regitems'];
    	$regitems = unserialize($regitems);
    	
    	if (!is_array($regitems))
    		$regitems = array();
    	 
    	$m_reg_items = array();
    	foreach ($regitems as $v){
    		if ($v['type'] <= 6){
    			$m_reg_items[] = $v;
    		}
    	}
    	
    	if ($this->getParam("newsletterkey") != null)
    	{
    		$change = array();
    		$change['newsletterkey'] = $this->getParam("newsletterkey");
    		$change['newslettervalue'] = trim($this->getParam("newslettervalue"));
    		
    		$obj_meet = new MeetModel($this->db);
    		$obj_meet->change($this->mid, $change);
    		
    		$this->view->setflag = true;
    		$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	}
    	
    	$this->view->minfo = $minfo;
    	$this->view->reg_items = $m_reg_items;
    	$this->render("setnl");
    }
    
    /**
     * 列表
     */
    public function nllistAction()
    {
    	$mid = $this->getParam("mid");
    
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($mid));
    	
    	$regitems = $minfo['regitems'];
    	$regitems = unserialize($regitems);
    	
    	if (!is_array($regitems))
    		$regitems = array();
    	
    	$m_reg_items = array();
    	foreach ($regitems as $v){
    		if ($v['type'] <= 6){
    			$m_reg_items[] = $v;
    		}
    	}
    	
    	$list = $this->db->fetchAll ('select t1.*,t2.* from meetuser t1
    			 left join user t2 on t2.uid=t1.uid
    			');
    	
    	$arr_list =array();
    	
    	if ($minfo['newsletterkey'] != '')
    	{
	    	foreach($list as $index => $v){
	    		$u_reg_item = $v['regitems'];
	    		$u_reg_item = unserialize($u_reg_item);
	    		$u_val = $u_reg_item[$minfo['newsletterkey']];
	    		if (strtolower(trim($u_val)) == strtolower(trim($minfo['newslettervalue']))){
	    			$arr_list[] = $v;
	    		}	    		
	    	}
    	}
    	
    	$this->view->list = $arr_list;
    	$this->render("nllist");
    }
    
    
    /**
     * 文件管理
     */
    public function fileAction()
    {
    	$uuid = $this->getParam("uuid");
    	$nowp = (int)$this->getParam("nowp");
    	
    	$fid = (int)$this->getParam("fid");
    	
    	if ($fid > 0)
    		$this->db->delete("mfile", "fid={$fid} and mid={$this->mid}");
    	
    	$per_page = 20;
    	$where = ' where mid=' . $this->mid . ' and uuid="' . $this->db->quote($uuid) . '"';
    	$totalNum = $this->db->fetchOne ('select count(*) from mfile ' . $where);
    	
    	require_once APP . '/application/models/Util.php';
    	    	
    	$pageInfo = Util::page ($totalNum, $per_page, $nowp);
    	$start    = $pageInfo ['start'];
    	$list     = $this->db->fetchAll ('select t1.*,t2.* from mfile t1 
    			left join user t2 on t2.uid=t1.uid' . $where .' order by fdate desc 
    			limit ' . $start . ',' . $per_page);

    	$this->view->uuid = $uuid;
    	$this->view->list = $list;
    	$this->view->page = $pageInfo['page'];
    	
    	$this->render("file");
    }
    
    
    /**
     * 批量文件下载
     */
    public function allfileAction()
    {
    	set_time_limit(0);
    	ini_set('memory_limit', '-1');
    	
    	$uuid = $this->getParam("uuid");
    	
    	$files = $this->db->fetchAll("select * from mfile where mid=? and uuid='?'", array($this->mid, $uuid));
    	
    	$minfo = $this->db->fetchRow("select * from meet where mid=?", array($this->mid));
    	
    	$menu_info = $this->getMenuByUUID($uuid, $minfo);
    	if (!empty($menu_info))
    		$dst_name = "/upload/meet/" . $this->mid . '/' . $menu_info['name'] . '_' . date("YmdHis") . '.zip';
    	else
    		$dst_name = "/upload/meet/" . $this->mid . '/' . $uuid . '.zip';
    	
    	if (file_exists(APP . $dst_name))
    		unlink(APP . $dst_name);
    	
    	$zip = new ZipArchive();
    	$ret = $zip->open(APP . $dst_name, ZIPARCHIVE::OVERWRITE);
    	
    	foreach($files as $v){
    		$isWin = false;
    		if (preg_match("/win/i", PHP_OS) != 0)
    			$isWin = true;
    		
    		if ($isWin)
    			$srcfiles = APP . iconv("UTF-8", "GBK", $v['filename']);
    		else
    			$srcfiles = APP . $v['filename'];
    		
    		$ret = $zip->addFile($srcfiles, iconv("UTF-8", "GBK", basename($v['filename'])));
    	}
    	$ret = $zip->close();
    	
    	$url = $this->view->fullUrl . $dst_name;
    	header("location:$url");
    }
    
    /**
     * 根据编号获取项
     * @param unknown_type $uuid
     * @return Ambigous <multitype:, unknown>
     */
    private function getMenuByUUID($uuid, $minfo)
    {
    	$top = $minfo['top'];
    	$top = unserialize($top);
    	$left = $minfo['left'];
    	$left = unserialize($left);
    	 
    	if (!is_array($top))$top = array();
    	if (!is_array($left)) $left = array();
    	$menu_all = array_merge($top, $left);
    	 
    	$arr_menu = array();
    	foreach($menu_all as $v){
    		if ($v['uuid'] == $uuid && $v['type'] == 'file'){
    			$arr_menu = $v;
    			break;
    		}
    	}
    	 
    	return $arr_menu;
    }
}